import 'package:flutter/widgets.dart';
import 'package:library_base/entities/in_order_page_child_entity.dart';
import 'package:library_base/entities/in_order_page_detail_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:get/get.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/date_util.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class IncomingOrdersController
    extends BaseListController<InOrderPageEntity, InOrderPageChildEntity> {


  DateTime? stationTimeBeginDateTime;

  WarehouseEntity? warehouse;

  Rx<String> keyWord = ''.obs;

  TextEditingController keyWorkSearchController = TextEditingController();


  @override
  void onCreate() {
    enableLoadMore = true;
    isShowNoMoreFooter = true;
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": page,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
    return super.requestData();
  }

  @override
  List<InOrderPageChildEntity> transformObject(InOrderPageEntity data, bool isRefresh) {
    return data.list;
  }

  void onChanged(String value){
    if(keyWord.value!= value){
      keyWord.value = value;
      update();
    }
  }

  void onSearch(String value){
    tryReLoad();
  }

  void filter(WarehouseEntity? warehouseEntity,DateTime? currentTime){
    stationTimeBeginDateTime = currentTime;
    warehouse = warehouseEntity;
    tryReLoad();
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": 0,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": page,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
  }

  void reset() {
    stationTimeBeginDateTime = null;
    warehouse = null;
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": page,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": 1,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
  }

  void backToList() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    keyWord.value = '';
    keyWorkSearchController.text = '';
    requestGeneralListData(Apis.IN_ORDER_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageNo": 1,
          "pageSize": limit,
          "createTimeBegin": stationTimeBeginDateTime==null?'':DateUtil.formatDate(stationTimeBeginDateTime,format: DataFormats.y_mo_d),
          "purchaseOrderNumber": keyWord.value,
          "warehouseId": warehouse==null?null:warehouse!.id,
        });
  }


  void toDetail(InOrderPageChildEntity itemData, int position) {
    Get.toNamed(Routers.INCOMING_ORDERS_DETAIL_PAGE,
        arguments: {'stockInId': itemData.stockInId,'callback':_updateUi});  }

  void _updateUi(){
    tryReLoad();
  }

}
